package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.AuthorityDAO;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.model.Authority;
import org.springframework.stereotype.Repository;

@Repository
public class AuthorityDAOImpl extends BaseDao implements AuthorityDAO {

	@Override
	public void save(Authority authority) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", authority.getId());
		map.put("name", authority.getName());
		map.put("description", authority.getDescription());
		map.put("enabled", authority.getEnabled());
		this.getPagableSqlSessionTemplate().insert("insertAuthority", map);
	}

	@Override
	public void delete(Authority authority) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", authority.getId());
		map.put("name", authority.getName());
		map.put("description", authority.getDescription());
		map.put("enabled", authority.getEnabled());
		this.getPagableSqlSessionTemplate().delete("deleteAuthority", map);
	}

	@Override
	public Authority findById(int id) {
		Authority authority = this.getPagableSqlSessionTemplate().selectOne(
				"findById", id);
		return authority;
	}

	@Override
	public List<Authority> findByProperty(String propertyName, Object value) {
		List<Authority> list = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", propertyName);
		map.put("value", value);
		list = this.getPagableSqlSessionTemplate().selectList("findByProperty",
				map);
		return list;
	}

	@Override
	public List<Authority> findByName(String name) {
		List<Authority> list = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "name");
		map.put("value", name);
		list = this.getPagableSqlSessionTemplate()
				.selectList("findByName", map);
		return list;
	}

	@Override
	public List<Authority> findByEnabled(Boolean enabled) {
		List<Authority> list = this.getPagableSqlSessionTemplate().selectList(
				"findByEnabled", enabled ? 1 : 0);
		return list;
	}

	@Override
	public List<Authority> findAll() {
		List<Authority> list = this.getPagableSqlSessionTemplate().selectList(
				"findAll");
		return list;
	}

	@Override
	public List<Authority> findAuthoritiesByRoleId(int roleId) {
		List<Authority> list = this.getPagableSqlSessionTemplate().selectList(
				"findAuthoritiesByRoleId", roleId);
		return list;
	}

	@Override
	public int saveAuthority(Authority authority) {
		return this.getPagableSqlSessionTemplate().insert("saveAuthority",
				authority);
	}

	@Override
	public int deleteAuthorities(List<Integer> ids) {
		return this.getPagableSqlSessionTemplate().delete("deleteAuthorities",
				ids);
	}

	@Override
	public PaginatedList<Authority> findAllAuthorities(int start, int limit,
			Map<String, String> map) {
		PreparedQuery<Authority> pq = this.getPagableSqlSessionTemplate()
				.findAll("findAllAuthorities", map);
		PaginatedList<Authority> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public int saveAuthoritySource(Map<String, String> map) {
		return this.getPagableSqlSessionTemplate().insert(
				"saveAuthoritySource", map);
	}

	@Override
	public int deleteAuthorityResource(int auId) {
		return this.getPagableSqlSessionTemplate().delete(
				"deleteAuthorityResource", auId);
	}

	@Override
	public Authority findAuthorityByName(String name) {
		return this.getPagableSqlSessionTemplate().selectOne("findAuthorityByName", name);
	}
}
